import customDataSource from './custom_dataSource.js';
import customWhere from './custom_where.js';
import customJqgrid from './custom_jqgrid.js';
import customMenu from './custom_menu.js';
import customTab from './custom_tab.js';
import customForm from './custom_form.js';

let urlHash=window.location.hash;

if(urlHash.split("custom=").length>1){
    let scrname = urlHash.split("custom=")[1].split("&")[0]
    let params = {"jn":JSON.stringify({"arg":{"scrname":scrname}})}
    customDataSource.ScreenBizData = customDataSource.getScreenBizDefault(params)
}


let custom = {
    ScreenData:{},  //自定义总数据
    boxId:'',   //父级DIV  例：'#customBox'

    init:function(ScreenData,param,callback){
        //常用参数
        let boxId = param['boxId'] || '';       //组件盒子ID
        let onlyInitTemplate = param['onlyInitTemplate'] || '';       //仅加载模板
        let onlyInitComponent = param['onlyInitComponent'] || '';       //仅加载组件

        this.boxId = boxId;
        this.ScreenData = ScreenData;
        this.customDataSource = customDataSource;
        this.customWhere = customWhere;
        this.customJqgrid = customJqgrid;
        this.customMenu = customMenu;
        this.customTab = customTab;
        this.customForm = customForm;

        if(onlyInitTemplate == "yes"){
            this.initTemplate();
        }else if(onlyInitComponent == "yes"){
            this.initComponent();
        }else{
            this.initTemplate();
            this.initComponent();
        }
        
    },

    //初始化模块框架
    initTemplate:function(callback){
        let _this = this;
        var template_arr = _this.ScreenData.template;
        $.each(template_arr,function(key,value){
            var html = '<div id="'+value.container+'" class="custom-template" style="position:absolute;top:'+value.c_top+';bottom:'+value.c_bottom+';left:'+value.c_left+';right:'+value.c_right+';width:'+value.width+';height:'+value.height+';border:0px solid #eeeeee"></div>';

            $(_this.boxId).append(html)
        })
    },

    //初始化模块内组件
    initComponent:function(callback){
        let _this = this;
        var component_arr = _this.ScreenData.component;
        $.each(component_arr,function(key,value){
            var container = value.container;
            var cptype = value.cptype;
            var cpname = value.cpname;

            switch (cptype) {
                case 'menu':
                    var param = {};
                    param.menuBoxId = container.indexOf("#") >= 0 ?container:"#"+container;
                    param.cpname = cpname;
                    var data = [];
                    $.each(_this.ScreenData.component_menu,function(kk,vv){
                        if(vv.cpname == cpname){
                            data.push(vv)
                        }
                    })
                    _this.initMenu(data,param)
                    break;
            
                case 'tab':
                    var param = {};
                    param.menuBoxId = container.indexOf("#") >= 0 ?container:"#"+container;;
                    param.cpname = cpname;
                    var data = [];
                    $.each(_this.ScreenData.component_tab,function(kk,vv){
                        if(vv.cpname == cpname){
                            data.push(vv)
                        }
                    })
                    _this.initTab(data,param)
                    break;
                case 'form':
                    var param = value;
                    param.menuBoxId = container.indexOf("#") >= 0 ?container:"#"+container;;
                    param.cpname = cpname;
                    var data = [];
                    $.each(_this.ScreenData.component_form,function(kk,vv){
                        if(vv.cpname == cpname){
                            data.push(vv)
                        }
                    })
                    _this.initForm(data,param)
                    break;
                case 'grid':
                    var param = value;
                    param.menuBoxId = container.indexOf("#") >= 0 ?container:"#"+container;;
                    param.cpname = cpname;
                    var data = [];
                    $.each(_this.ScreenData.component_grid,function(kk,vv){
                        if(vv.cpname == cpname){
                            data.push(vv)
                        }
                    })
                    _this.initJqgrid(data,param)
                    break;
                default:
                    break;
            }
        })
    },

    //初始化菜单
    initMenu:function(data,param,callback){
        let _this = this;
        let ScreenData = _this.ScreenData;
        custom.initMenu_data = data;
        custom.initMenu_param = param;
        customMenu.initHtml(ScreenData,param,function(){
            customMenu.initMenuContent(custom,data,param,function(){

            })
        })
    },

    //初始化tab
    initTab:function(data,param,callback){
        let _this = this;
        let ScreenData = _this.ScreenData;
        custom.initTab_data = data;
        custom.initTab_param = param;
        customTab.initHtml(ScreenData,param,function(){
            customTab.initTabContent(data,param,function(){

            })
        })
    },

    //初始化表单
    initForm:function(data,param,callback){
        let _this = this;
        let ScreenData = _this.ScreenData;
        custom.initForm_data = data;
        custom.initForm_param = param;
        customForm.initHtml(ScreenData,param,function(){
            customForm.initFormContent(data,param,function(){

            })
        })
    },

    //初始化jqgrid
    initJqgrid:function(data,param,callback){
        let _this = this;
        let ScreenData = _this.ScreenData;
        custom.initJqgrid_data = data;
        custom.initJqgrid_param = param;

        customJqgrid.initHtml(ScreenData,param,function(){
            customJqgrid.initJqgrid(data,param,function(){

            })
        })
    },

}

//导出模块
export {custom as custom,customDataSource as C_DataSource,customWhere as C_Where,customJqgrid as C_Grid}